package ui.activity.mine;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import com.dispatchapp.andlei.R;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import base.BaseBean;
import base.activity.BaseLayoutActivity;
import bean.NoticeConfigBean;
import netWork.NetUtils;
import utils.GsonUtils;
import utils.SPUtils;

/**
 * author : xulei
 * e-mail : 1352267014@qq.com
 * date   : 2022/2/21  20:56
 * desc   :
 */
public class NotificationSettingActivity extends BaseLayoutActivity {
    private Switch switch_message, switch_phone ,switch_message_only;
    private TextView tv_content;
    private NetUtils netUtils;
    private int msg_type = 0;
    private int voice_open =0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("通知设置");
        setTitleBg(R.color.color_333333);
        setTitlefontBg();
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_notification_setting;
    }

    @Override
    protected void findViews() {
        switch_message_only = getView(R.id.switch_message_only);
        switch_message = getView(R.id.switch_message);
        switch_phone = getView(R.id.switch_phone);
        tv_content = getView(R.id.tv_content);
        if(SPUtils.getInstance(mActivity).getBoolean("switch_message_only",false)){
            switch_message_only.setChecked(true);
        }else {
            switch_message_only.setChecked(false);
        }
    }

    @Override
    protected void formatViews() {
        netUtils = new NetUtils(mActivity,netRequestCallBack);
        switch_message.setOnClickListener(this);
        switch_phone.setOnClickListener(this);
        switch_message_only.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    SPUtils.getInstance(mActivity).put("switch_message_only",true);
                }else {
                    SPUtils.getInstance(mActivity).put("switch_message_only",false);
                }
            }
        });
        switch_message.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    msg_type = 1;
                }else {
                    msg_type = 0;
                }
            }
        });
        switch_phone.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    voice_open = 1;
                }else {
                    voice_open = 0;
                }
            }
        });

    }

    @Override
    protected void formatData() {
        loadNotice_Config();
    }

    @Override
    protected void getBundle(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.switch_message:
            case R.id.switch_phone:
                UpdateNotifce_Config();
                break;
        }
    }

    /**
     *配送区域 - 通知设置
     */
    private void loadNotice_Config(){
        Map<String, Object> map = new HashMap<>();
        netUtils.post("rider/user/notice-config", map, BaseBean.class, false);
    }

    /**
     * 修改 通知设置
     */
    private void UpdateNotifce_Config(){
        // msg_type 	Int 短信通知 0:未选择， 1： 勾选， 2: 不勾选
        // voice_open 	Int 电话通知开关， 0:关闭， 1:开启
        Map<String, Object> map = new HashMap<>();
        map.put("msg_type",""+msg_type);
        map.put("voice_open",""+voice_open);
        netUtils.post("rider/user/update-notice-config", map, BaseBean.class, false);
    }

    private NetUtils.NetRequestCallBack netRequestCallBack = new NetUtils.NetRequestCallBack() {
        @Override
        public void error(String action, Throwable e, Map tag) {
        }

        @Override
        public void success(String action, BaseBean t, Map tag) {
            switch (action){
                case "rider/user/notice-config":
                    if (t.getCode() != 200) {
                        toast(t.getMsg());
                    } else {
                        NoticeConfigBean bean = (NoticeConfigBean) GsonUtils.getInstance(mActivity).parseComplexJArrayByCommon(t,NoticeConfigBean.class);
                        if(bean.getMsg_type() == 1){
                            switch_message.setChecked(true);
                        }else {
                            switch_message.setChecked(false);
                        }
                        if (bean.getVoice_open() == 1){
                            switch_phone.setChecked(true);
                        }else {
                            switch_phone.setChecked(false);
                        }
                        List<NoticeConfigBean.TemplatesBean> templatesBeans = bean.getTemplates();
                        if(templatesBeans.size()>0){
                            tv_content.setText(templatesBeans.get(0).getContent());
                        }else {
                            tv_content.setText("暂无短信通知模板内容");
                        }

                    }
                    break;
                case "rider/user/update-notice-config":
                    if (t.getCode() != 200) {
                        toast(t.getMsg());
                        loadNotice_Config();
                    }
                    break;
            }

        }
    };

}
